package ft.xpf.util;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONPath;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class JsonParseUtil
{
	/**
	 * 输出 Pretty 格式
	 * @param txt
	 * @return
	 * @throws Exception
	 */
	public static String jsonFormatPretty(String txt) throws Exception
	{
		String ret ="";
		
		if(txt!=null && txt.length()>0)
		{
			Object ob = JSON.parse(txt);
			ret = JSON.toJSONString(ob, SerializerFeature.PrettyFormat);
		}
		
		return ret;
	}
	
	/**
	 * 输出紧凑格式
	 * @param txt
	 * @return
	 * @throws Exception
	 */
	public static String jsonFormatCompact(String txt) throws Exception
	{
		String ret ="";
		
		if(txt!=null && txt.length()>0)
		{
			Object ob = JSON.parse(txt);
			ret = JSON.toJSONString(ob);
		}
		
		return ret;
	}
	
	/**
	 * 根据JSONPath查找特定的值
	 * @param str_json
	 * @param xpath
	 * @return
	 * @throws Exception
	 */
	public static String jsonSelectSingle(String str_json, String xpath)
			throws Exception
	{
		String ret = "";
		
		Object ob = JSON.parse(str_json);
		Object ob_out = null;
		
		ob_out = JSONPath.eval(ob, xpath);
		
		if(ob_out!=null )
		{
			ret = ob_out.toString();
		}
		
		return ret;
	}
}
